package cc.yugu.yhdw.webserver.web.controller.security.hdw;

import cc.yugu.yhdw.common.annotation.ViewModel;
import cc.yugu.yhdw.common.domain.hdw.BatteryPack;
import cc.yugu.yhdw.common.domain.hdw.Supplier;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.json.PageResult;
import cc.yugu.yhdw.webserver.constant.AppConstEnum;
import cc.yugu.yhdw.webserver.service.hdw.BatteryPackService;
import cc.yugu.yhdw.webserver.service.hdw.SupplierService;
import cc.yugu.yhdw.webserver.web.controller.security.SecurityController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/security/hdw/battery_pack")
public class BatteryPackController extends SecurityController {
    @Autowired
    BatteryPackService batteryPackService;
    @Autowired
    SupplierService supplierService;

    @RequestMapping(value = "index.htm")
    public void index(Model model) {
        List<Supplier> supplierList = supplierService.findByType(Supplier.Type.BATTERY.getValue());
        model.addAttribute("supplierList", supplierList);
        model.addAttribute(MENU_CODE_NAME, AppConstEnum.Menu.MENU_03_01.getValue());
    }

    @RequestMapping(value = "page.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public PageResult page(BatteryPack batteryPack) {
        return PageResult.successResult(batteryPackService.findPage(batteryPack));
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("add.htm")
    public String add(Model model) {
        model.addAttribute("supplierList", supplierService.findByType(Supplier.Type.BATTERY.getValue()));
        return "/security/hdw/battery_pack/add";
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping(value = "edit.htm")
    public String edit(int id, Model model) {
        BatteryPack smsConfig = batteryPackService.find(id);
        model.addAttribute("entity", smsConfig);
        model.addAttribute("supplierList", supplierService.findByType(Supplier.Type.BATTERY.getValue()));
        return "/security/hdw/battery_pack/edit";
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "create.htm")
    public ExtResult create(BatteryPack pack) {
        return batteryPackService.insert(pack);
    }

    @ResponseBody
    @ViewModel(ViewModel.JSON)
    @RequestMapping(value = "update.htm")
    public ExtResult update(BatteryPack entity) {
        return batteryPackService.update(entity);
    }

    @RequestMapping(value = "view.htm")
    public void view(Model model, int id) {
        model.addAttribute("id", id);
    }

    @RequestMapping(value = "view_basic_info.htm")
    public String viewBasicInfo(Model model, int id) {
        BatteryPack entity = batteryPackService.find(id);
        if (entity == null) {
            return SEGMENT_RECORD_NOT_FOUND;
        } else {
            model.addAttribute("entity", entity);
        }
        return "/security/hdw/battery_pack/view_basic_info";
    }

    @RequestMapping(value = "view_battery.htm")
    public void viewBattery(Model model, int id) {
        model.addAttribute("packId", id);
    }

    @RequestMapping("delete.htm")
    @ViewModel(ViewModel.JSON)
    @ResponseBody
    public ExtResult delete(int id) {
        return batteryPackService.delete(id);
    }

    @ViewModel(ViewModel.INNER_PAGE)
    @RequestMapping("select_battery_pack.htm")
    public void selectBatteryPack() {
    }

}

